package com.example.chapter7.servlet;

import com.skycode7.bean.AdminBean;
import com.skycode7.dao.AdminDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/AdminAdd.jsp")
public class AdminAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String adminName = req.getParameter("AdminName");
        String adminPwd = req.getParameter("AdminPwd");
        AdminBean admin = new AdminBean();//实例化一个admin对象
        //设置admin对象属性值
        admin.setAdminName(adminName);
        admin.setAdminPwd(adminPwd);
        admin.setLastLoginTime((new Date()).toString());
        admin.setAdminType(0);
        AdminDao AdminDao = new AdminDao();
        int result = AdminDao.add(admin);
        if (result > 0){
            //补充知识 跳转到网页admin.html中
            resp.sendRedirect("news.html");//使用结果对象 完成页面网址跳转
        }else {//失败显示错误提示页面
            req.getRequestDispatcher("WEB-INF/error.jsp").forward(req,resp);
        }
    }
}
